package com.example.blogother.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.plugins.pagination.PageDTO;
import com.example.blogother.mapper.ArticleMapper;
import com.example.blogother.mapper.FailMapper;
import com.example.blogother.mapper.ShenMapper;
import com.example.blogother.mapper.UserinfoMapper;
import com.xpd.blog.bean.*;
import org.apache.ibatis.executor.BatchResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xpd
 * @since 2024-11-01
 */
@RestController
@RequestMapping("/shen")
public class ShenController {
    @Autowired
    ShenMapper shenMapper;
    @Autowired
    UserinfoMapper userinfoMapper;
    @Autowired
    FailMapper failMapper;
    @Autowired
    ArticleMapper articleMapper;
    @RequestMapping("getShen")
    public Result getShen(@RequestParam int page, @RequestParam int size, @RequestBody Shen shen,@RequestParam String searchName){
        LambdaQueryWrapper<Shen>lqw=new LambdaQueryWrapper<>();
        /*if(searchName!=null&&!"".equals(searchName)){
            LambdaQueryWrapper<Userinfo> ulqw=new LambdaQueryWrapper<>();
            LambdaQueryWrapper<Userinfo> user = ulqw.like(Userinfo::getUname,searchName);
            lqw.in(Shen::getUsid,user);
        }*/
        if(searchName!=null&&!"".equals(searchName)){
            System.out.println("我执行了");
            LambdaQueryWrapper<Userinfo> ulqw=new LambdaQueryWrapper<>();
            LambdaQueryWrapper<Userinfo> user = ulqw.like(Userinfo::getUname,searchName);
            lqw.in(Shen::getUsid,user);
        }
        if(shen.getStatus()!=null&&!"".equals(shen.getStatus())){
            lqw.eq(Shen::getStatus,shen.getStatus());
        }
        Page p=new PageDTO(page,size);
        Page page1 = shenMapper.selectPage(p, lqw);
        List records = page1.getRecords();
        for (Object record : records) {
            Shen record1 = (Shen) record;
            LambdaQueryWrapper<Userinfo> lambdaQueryWrapper=new LambdaQueryWrapper<>();
            LambdaQueryWrapper<Userinfo> eq = lambdaQueryWrapper.eq(Userinfo::getUsid, record1.getUsid());
            List<Userinfo> userinfos = userinfoMapper.selectList(lambdaQueryWrapper);
            Fail fail = failMapper.selectById(record1.getFid());
            Article article = articleMapper.selectById(fail.getAid());
            record1.setArticle(article);
            record1.setUserinfo(userinfos.get(0));
        }
        return new Result(1,"查询成功",page1);
    }
    //修改状态
    @RequestMapping("updateShen")
    public Result updateShen(@RequestParam int  sid,@RequestParam int status){
        LambdaQueryWrapper<Shen> lqw=new LambdaQueryWrapper<>();
        lqw.eq(Shen::getSid, sid);
        List<Shen> shens = shenMapper.selectList(lqw);
        shens.get(0).setStatus(status);
        int i = shenMapper.updateById(shens.get(0));
        if(status==2){
            Fail fail = failMapper.selectById(shens.get(0).getFid());
            Article article = articleMapper.selectById(fail.getAid());
            article.setStatus(0);
            int i1 = articleMapper.updateById(article);
        }
        if(i>0){
            return new Result(1,"审核成功",null);
        }else{
            return new Result(-1,"审核失败",null);
        }

    }
    //插入数据
    @RequestMapping("insertShen")
    public Result insertShen(@RequestBody Shen shen){
        shen.setTime(LocalDateTime.now());
        int insert = shenMapper.insert(shen);
        return new Result(1,"插入成功",insert);
    }
    //得到所有的申诉
    @RequestMapping("getAllShen")
    public Result getAllShen(){
        List<Shen> shens = shenMapper.selectList(null);
        return new Result(1,"查询成功",shens);
    }

}

